package com.yytstudy.bd_tts_demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.speechsynthesizer.SpeechSynthesizer;
import com.baidu.speechsynthesizer.SpeechSynthesizerListener;
import com.baidu.speechsynthesizer.publicutility.SpeechError;

public class MainActivity extends Activity implements OnClickListener{

	private EditText etText;
	private Button btnStart;
	SpeechSynthesizer speechSynthesizer ;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initTTS();
	}

	/**
	 * @Title: initView
	 * @Description: 初始化控件
	 * @param 设定文件
	 * @author username
	 * @return void 返回类型
	 * @throws
	 */

	private void initView()
	{
		etText = (EditText) findViewById(R.id.ed_text);
		btnStart=(Button) findViewById(R.id.btn_start);
		
		btnStart.setOnClickListener(this);

	}

	private void initTTS()
	{
		MySpeechSynthesizerListener myListener = new MySpeechSynthesizerListener();
		 speechSynthesizer = new SpeechSynthesizer(getApplicationContext(), "fuck",myListener );
		speechSynthesizer.setApiKey(StaticData.API_KEY, StaticData.SERECT_KEY);
		//设置发音为女生
		speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, SpeechSynthesizer.SPEAKER_FEMALE);
		//音量为5
		speechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "5");
		//速度为5
		speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");
		//音调为5
		speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5");
		//设置音频格式为arm压缩格式
		speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE, SpeechSynthesizer.AUDIO_ENCODE_AMR);
		//设置音频比特率为amr 15.85k比特率
		speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE, SpeechSynthesizer.AUDIO_BITRATE_AMR_15K85);
		
	}

	
	/**
	  * @ClassName: MySpeechSynthesizerListener
	  * @Description: 	语音合成器回调接口
	  * @author username
	  * @date 2015-5-29 下午8:33:05
	  */
	
	class MySpeechSynthesizerListener implements SpeechSynthesizerListener{

		
		//缓冲进度已更新（尚未支持）
		@Override
		public void onBufferProgressChanged(SpeechSynthesizer arg0, int arg1)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onBufferProgress……");
			
		}
		
		/**
		  * <p>已取消（合成取消，朗读停止）</p>
		  * @param arg0  合成器对象
		  */
		
		@Override
		public void onCancel(SpeechSynthesizer arg0)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onCancel……");
			
		}
		
		
		/**
		  * <p>发生错误</p>
		  * @param arg0 合成器对象
		  * @param arg1 错误代码
		  */
		
		@Override
		public void onError(SpeechSynthesizer arg0, SpeechError arg1)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onError……");
			
		}

		
		/**
		  * <p>合成器返回了新的语音数据: </p>
		  * @param arg0 合成器对象
		  * @param arg1 语音数据buffer
		  * @param arg2 是否是最后一段数据
		  */
		
		@Override
		public void onNewDataArrive(SpeechSynthesizer arg0, byte[] arg1, boolean arg2)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onNewDataArrive……");
			
		}

		
		/**
		  * <p>朗读完成: </p>
		  * @param arg0 合成器对象
		  */
		
		@Override
		public void onSpeechFinish(SpeechSynthesizer arg0)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onSpeechfinsh……");
			
		}

		
		/**
		  * <p>朗读已暂停 </p>
		  * @param arg0 合成器对象
		  */
		
		@Override
		public void onSpeechPause(SpeechSynthesizer arg0)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onSpeechPause……");
			
		}

		
		/**
		  * <p>朗读进度已更新（尚未支持） </p>
		  * @param arg0 合成器对象
		  * @param arg1  已缓冲的进度，取值范围[0, 100]
		  */
		
		@Override
		public void onSpeechProgressChanged(SpeechSynthesizer arg0, int arg1)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onSpeechProgress……");
			
		}

		
		/**
		  * <p>朗读已继续: </p>
		  * @param arg0 朗读已继续
		  */
		
		@Override
		public void onSpeechResume(SpeechSynthesizer arg0)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onSpeechResume……");
			
		}

		
		/**
		  * <p>合成器开始朗读: </p>
		  * @param arg0 合成器对象
		  */
		
		@Override
		public void onSpeechStart(SpeechSynthesizer arg0)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onSpeechStart……");
			
		}

		
		/**
		  * <p>合成器开始工作: </p>
		  * @param arg0  合成器对象
		  */
		
		@Override
		public void onStartWorking(SpeechSynthesizer arg0)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onStartWorking……");
			
		}

		@Override
		public void onSynthesizeFinish(SpeechSynthesizer arg0)
		{
			// TODO Auto-generated method stub
			Log.i("info", "onSynthesizer……");
			
		}

		
	}


	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.btn_start:
//			initTTS();
			speechSynthesizer.speak(etText.getText().toString());
			break;

		default:
			break;
		}
		
		
	}
}
